package com.hk.admin.spring.config;

import com.hk.admin.spring.security.CustomUserDetailsService;
import com.hk.admin.spring.security.RestAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * <br>
 * <b>功能：</b>Security登录配置，HTTP基本认证(Basic Authentication)<br>
 * <b>作者：</b>Pan.ShiJu<br>
 * <b>日期：</b>2017/4/11 23:27<br>
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//允许进入页面方法前检验
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private HKAdminConfig hkAdminConfig;
    @Autowired
    private FileUploadConfig fileUploadConfig;

    /**
     * 密码加密、解密bean，此处加入spring容器中，可以通过@Autowired自动注入
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * /注册CustomUserService的bean
     */
    @Bean
    UserDetailsService customUserDetailsService() {
        return new CustomUserDetailsService();
    }

    /**
     * 配置 入口点
     */
    @Bean
    RestAuthenticationEntryPoint restAuthenticationEntryPoint() {
        return new RestAuthenticationEntryPoint();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //添加我们自定的user detail service认证；
        auth.userDetailsService(customUserDetailsService()).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic()
                .authenticationEntryPoint(restAuthenticationEntryPoint())
                .and().logout().logoutUrl(hkAdminConfig.getLogoutUrl()).logoutSuccessUrl(hkAdminConfig.getLogoutSuccessUrl()) /* 登出请求配置 */
                .and().authorizeRequests().anyRequest().authenticated()
                .and().csrf().disable();

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        /**
         * 忽略的路径，不会权限校验
         */
        String[] ignoredPath = {
                "/",
                "/*.ico",
                "/*.js",
                "/*.css",
                "/*.html",
                "/assets/**",
                "/main/**",
                fileUploadConfig.getResourceHandler() /* 静态图片 */
        };
        web.ignoring().antMatchers(ignoredPath);
    }


}